package com.njust.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njust.constant.Constant;
import com.njust.domain.dto.VideoDTO;
import com.njust.domain.dto.VideoPageQueryDTO;
import com.njust.domain.po.PersonnelCapabilitie;
import com.njust.domain.po.Video;
import com.njust.domain.vo.PageQueryVO;
import com.njust.exception.UserHasCompletedAssessment;
import com.njust.exception.UserWithoutRecords;
import com.njust.exception.UserWithoutTrain;
import com.njust.exception.VideoNotFound;
import com.njust.mapper.PersonnelCapabilitieMapper;
import com.njust.mapper.VideoMapper;
import com.njust.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/13 22:37
 * @视频相关功能实现
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {


    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private PersonnelCapabilitieMapper personnelCapabilitieMapper;


    /**
     * 视频上传
     * @param videoDTO
     */
    public void uploadVideo(VideoDTO videoDTO) {
        //判断视频类型
        Integer video_status= videoDTO.getVideo_status();
        //如果是考核视频 需要查人员能力表
        if(video_status== Constant.OPERATE_VIDEO){
            //获取用户id
            Integer uId=videoDTO.getUId();
            Integer project_id=videoDTO.getProject_id();
            //根据用户id和项目id查询人员能力表的考核状态 状态为0上传失败 返回未培训 状态为1返回上传成功 状态为2返回已通过考核 无需再次上传
            PersonnelCapabilitie personnelCapabilitie = personnelCapabilitieMapper.findByUserIdAndProjectId(uId, project_id);

            if (personnelCapabilitie!= null) {
                //获取培训状态
                Integer assessment_status = personnelCapabilitie.getAssessment_status();

                if (assessment_status == Constant.WITHOUT_TRAIN) {
                    // 状态为0上传失败 返回未培训
                    throw new UserWithoutTrain("用户未培训，视频上传失败!请先观看培训视频完成培训！");
                } else if (assessment_status == Constant.Completed_TRAIN) {
                    // 状态为1返回上传成功
                    Video video=new Video();
                    BeanUtils.copyProperties(videoDTO,video);
                    videoMapper.insert(video);
                    log.info("视频上传成功");
                    //同时为人员能力表填充video_id字段
                    personnelCapabilitie.setVideo_id(video.getVideo_id());
                    personnelCapabilitieMapper.updatePersonnelCapabilitie(personnelCapabilitie);
                } else if (assessment_status == Constant.COMPLETED_ASSESSMENT) {
                    // 状态为2返回已通过考核 无需再次上传
                    throw new UserHasCompletedAssessment("用户已通过考核，无需再次上传视频");
                }
            } else {
                throw new UserWithoutRecords("未找到对应的人员能力记录，无法确定上传状态");
            }

        }else{//其余两种 直接插入视频表
            Video video=new Video();
            //对象属性拷贝
            BeanUtils.copyProperties(videoDTO,video);
            videoMapper.insert(video);
            log.info("视频上传成功");
        }
    }

    /**
     * 视频分页查询
     * @param videoPageQueryDTO
     * @return
     */
    public PageQueryVO pageQuery(VideoPageQueryDTO videoPageQueryDTO) {
        PageHelper.startPage(videoPageQueryDTO.getPage(), videoPageQueryDTO.getPageSize());

        Page<Video> page=videoMapper.pageQuery(videoPageQueryDTO);
        long total=page.getTotal();
        List<Video> records=page.getResult();

        return new PageQueryVO(total,records);
    }

    /**
     * 播放视频
     * @param video_id
     * @return
     */
    public String watchVideo(int video_id) {
        Video video=videoMapper.selectById(video_id);
        if(video==null){
            throw new VideoNotFound("未找到对应视频");
        }
        return video.getVideo_link();
    }
}
